//
//  UpdateDetailViewController.m
//  WideHelp
//
//  Created by ENERGY on 2018/8/7.
//  Copyright © 2018年 ENERGY. All rights reserved.
//首页-升级

#import "UpdateDetailViewController.h"
#import "JoinInstructionViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>

@interface UpdateDetailViewController ()<UIWebViewDelegate>

@property (strong, nonatomic) JSContext *context;

@end

@implementation UpdateDetailViewController(networking)
- (void)testData{
    [self showprogressHUD];
    [[AFAppDotNetAPIClient sharedClient] POST:@"test" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self hiddenProgressHUD];
        if ([responseObject[@"msg"] isEqualToString:@"success"]) {
            
        }else{
            ShowMessage(@"msg");
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self hiddenProgressHUD];
        [self showError:error];
    }];
}
@end
@implementation UpdateDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"计划详情";
    int nextPageBtnH = 44/WIDTH_6_SCALE;
    UIButton * nextPageBtn = [[UIButton alloc] initWithFrame:CGRectMake(15/WIDTH_6_SCALE, SCREEN_HEIGHT - NAVI_HEIGHT - 20/WIDTH_6_SCALE - BOTTOM_HEIGHT - nextPageBtnH, SCREEN_WIDTH - 30/WIDTH_6_SCALE, nextPageBtnH)];
    [nextPageBtn setTitle:@"立即升级" forState:UIControlStateNormal];
    nextPageBtn.titleLabel.font = DEF_FontSize_16;
    [nextPageBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextPageBtn setBackgroundColor:getColor(greenBGColor)];
    [nextPageBtn addTarget:self action:@selector(nextPageBtnClick) forControlEvents:UIControlEventTouchUpInside];
    nextPageBtn.layer.cornerRadius = 4;
    nextPageBtn.layer.masksToBounds = YES;
    [nextPageBtn setBackgroundImage:[UIImage imageNamed:@"maself_bg"] forState:UIControlStateNormal];
    [self.view addSubview:nextPageBtn];
    
    //接收H5进行跳转
    UIWebView * web = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    NSURL *url = [NSURL URLWithString:@"http://bobang.sencant.com/index.php/test"];//创建URL
    NSURLRequest* request = [NSURLRequest requestWithURL:url];//创建
    [web loadRequest:request];//加载
    // 需要在代理方法中与js进行交互
    web.delegate = self;
    [self.view addSubview:web];

    self.context = [web valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];
    // 打印异常,由于JS的异常信息是不会在OC中被直接打印的,所以我们在这里添加打印异常信息,
    self.context.exceptionHandler =
    ^(JSContext *context, JSValue *exceptionValue){
        context.exception = exceptionValue;
    };
    
    __weak typeof (self)weakSelf = self;
    //注销登录
    self.context[@"nextPageBtnClick"] = ^(){
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"注销登陆" message:@"是否注销登录" preferredStyle:UIAlertControllerStyleAlert];
        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }]];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [weakSelf nextPageBtnClick];//OC的注销方法
        }]];
        [weakSelf presentViewController:alertController animated:YES completion:nil];
    };
}

- (void)nextPageBtnClick{
    JoinInstructionViewController * vc = [[JoinInstructionViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
}
@end
